package com.gem.day11;

import java.util.Comparator;

/**
 * 比较器工厂
 */
public class PersonComparatroFactory {

	public static final int AGE_MAX_TO_MIN = 1;
	public static final int AGE_MIN_TO_MAX = 2;
	public static final int NAME_MAX_TO_MIN = 3;
	public static final int NAME_MIN_TO_MAX = 4;

	public static Comparator<Person> getComparator(int type) {
		Comparator<Person> comparator = null;
		if (type == AGE_MAX_TO_MIN) {
			comparator = (o1, o2) -> {
				if (o1.getAge() > o2.getAge()) {
					return -1;
				} else if (o1.getAge() < o2.getAge()) {
					return 1;
				}
				return 0;
			};
		} else if (type == AGE_MIN_TO_MAX) {
			comparator = (o1, o2) -> {
				if (o1.getAge() > o2.getAge()) {
					return 1;
				} else if (o1.getAge() < o2.getAge()) {
					return -1;
				}
				return 0;
			};
		} else if (type == NAME_MAX_TO_MIN) {
			comparator = (o1, o2) -> o2.getName().compareTo(o1.getName());
		} else if (type == NAME_MIN_TO_MAX) {
			comparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
		} else {
			throw new RuntimeException("参数不合法...");
		}
		return comparator;
	}
}
